/*
Realistic Unit Modification v4.0 Dialog 
v4.0.0.1 [ALPHA]
Concept & Development by Draper & Raps | [VRC]
*/

private ["_num","_helo","_callsign","_spwn","_mkrname","_type","_hint","_asloc","_stgtar","_mkrname2","_pilot","_grp","_mkrnamepri","_pos1","_pos2","_dis","_dir","_wpAH","_station","_mkrname1","_mkrname3"];

/* variables */
_num = count _this;
_helo = _this select (_num - 1);
_callsign = _this select 1;
_spwn = _this select 3;

// _paytar = [];
RUM_ASclick = false;

/* open map for marker and location selection */
openMap [true,true];
for [{_n = 1},{_n <= 3},{_n = _n + 1}] do {
	_mkrname = "";
	_type = "";
	_hint = "";
	switch (_n) do {
		case 1: {
			_mkrname = format ["%1target",_helo];
			_type = "Destroy";
			_hint = "Select Target Area for CAS";
		};
		case 2: {
			_mkrname = format ["%1ingress",_helo];
			_type = "mil_start";
			_hint = "Select Ingress Point";
		};
		case 3: {
			_mkrname = format ["%1egress",_helo];
			_type = "mil_end";
			_hint = "Select Egress Point";
		};
		default {
			hint "shit";	
		};
	};
	_asloc = createMarkerLocal [_mkrname, [0,0,0]];
	_asloc setMarkerShapeLocal "ICON";
	_mkrname setMarkerTypeLocal _type;

	if (_n == 1) then {
		// _stgtar = format ["'%1' setMarkerPosLocal _pos,RUM_ASclick = [_pos] call ROEzone",_mkrname];
	_mapclick = ["rum_mapclick_id", "onMapSingleClick", { _this setMarkerPosLocal _pos;RUM_ASclick = [_pos] call ROEzone; }, _mkrname] call BIS_fnc_addStackedEventHandler;
	} else {
		// _stgtar = format ["'%1' setMarkerPosLocal _pos,RUM_ASclick = true",_mkrname];
	_mapclick = ["rum_mapclick_id", "onMapSingleClick", { _this setMarkerPosLocal _pos;RUM_ASclick = true; }, _mkrname] call BIS_fnc_addStackedEventHandler;
	};
	
	titleText [_hint, "PLAIN DOWN"];
	waitUntil {RUM_ASclick};
	
	_mapclick = ["rum_mapclick_id", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;
	if((_n == 2) or (_n == 3)) then {
		_mkrname2 = format ["%1target",_helo];
		_mkrname setMarkerDir ((getMarkerPos _mkrname2 select 0)-(getMarkerPos _mkrname select 0)) atan2 ((getMarkerPos _mkrname2 select 1)-(getMarkerPos _mkrname select 1));
	};
	RUM_ASclick = false;
};

/* close map */
titleFadeOut 0;
sleep 1;
openMap [false,false];

/* create pilot and gunner */
_grp = createGroup WEST;
_pilot = _grp createUnit [RUM_AHEL_Pilot, getpos _spwn, [], 0, "FORM"];
_pilot moveinDriver _helo;
_gunner = _grp createUnit [RUM_AHEL_Pilot, getpos _spwn, [], 0, "FORM"];
_gunner moveinGunner _helo;

/* removes any units that are extra */
if (!(_gunner in crew _helo)) then {
	deleteVehicle _gunner;
};

/* calculates distance from the target to the player */
_mkrnamepri = format ["%1target",_helo];
_pos1 = getpos player;
_pos2 = getMarkerpos _mkrnamepri;
_dis = (round ((_pos2 distance _pos1)/10))*10;
_dir = round (((_pos1 select 1)-(_pos2 select 1)) atan2 ((_pos1 select 0)-(_pos2 select 0)));

if (_dir < 0) then {
	_dir = _dir + 360;
};

/* dialog from pilot to player */
["5line",[player,_pilot,_callsign,_dir,_dis]] execVM (RUM_PATH+"AHel\radiodialogs.sqf");
sleep 30;

/* creates the waypoints to perform */
for [{_n = 1},{_n <= 3},{_n = _n + 1}] do {
	_mkrname = "";
	switch (_n) do {
		case 1: {
			_mkrname = format ["%1ingress",_helo];
		};
		case 2: {
			_mkrname = format ["%1target",_helo];
		};
		case 3: {
			_mkrname = format ["%1egress",_helo];
		};
		default {
			hint "shit";	
		};
	};
	if (_n == 2) then {
		_wpAH =_grp addWaypoint [getMarkerpos _mkrname, 0];
		_wpAH setWaypointType "SAD";
		_wpAH setWaypointSpeed "FULL";
		_wpAH setWaypointCombatMode "RED";
		_wpAH setWaypointCompletionRadius 700;	
	} else {	
		_wpAH=_grp addWaypoint [getMarkerpos _mkrname, 0];
		_wpAH setWaypointType "MOVE";
		_wpAH setWaypointSpeed "NORMAL";
		_wpAH setWaypointCombatMode "BLUE";
		_wpAH setWaypointCompletionRadius 50;
	};
};

/* once cas missin complete, goes back to station */
_station = format ["%1station",_helo];

_wpAH =_grp addWaypoint [getMarkerpos _station, 0];
_wpAH setWaypointType "MOVE";
_wpAH setWaypointSpeed "NORMAL";
_wpAH setWaypointBehaviour "CARELESS";
_wpAH setWaypointCombatMode "BLUE";
_wpAH setWaypointCompletionRadius 50;

sleep random(30);

/* waits till the helo has returned to station before deleting markers */
waitUntil{(!alive _helo) or (((((getPos _helo select 0) - (getmarkerpos _station select 0))^2+((getPos _helo select 1) - (getmarkerpos _station select 1))^2)^0.5) <= 200)};

/* remove markers created for mission */
_mkrname1 = format ["%1ingress",_helo];
_mkrname2 = format ["%1target",_helo];
_mkrname3 = format ["%1egress",_helo];
deletemarkerLocal _mkrname1;
deletemarkerLocal _mkrname2;
deletemarkerLocal _mkrname3;

/* exits script */
if(true)exitwith{};
